unit clsProd;

interface

uses dbtables,db,dcatalogo,extctrls,graphics,classes,jpeg;

type TProd=Class
       private
         cod_prod:string;
       public
         cod_cat  :string;
         descricao:String;
         carac    :string;
         function gravar(pcod_prod:string; var pjpeg:TJpegImage):byte;
         function recuperar(pcod_prod:string; var pjpeg:TJpegImage):byte;
         function excluir(pcod_prod:string):byte;
         procedure lista(var pq:TQuery; pdescricao:string);
       end;

implementation

function TProd.gravar;
var q:TQuery;
    ms:TMemoryStream;

begin
  ms:=TmemoryStream.Create;
  pJpeg.savetostream(ms);
  ms.position:=0;
  q:=TQuery.Create(dm.db);
  q.Name:='prod';
  q.DatabaseName:=dm.db.DatabaseName;
  q.Close;
  q.SQL.Clear;
  q.Params.Clear;
  q.SQL.Add('SELECT * FROM PROD WHERE COD_PROD=:KCOD_PROD');
  q.ParambyName('kcod_prod').asstring:=pcod_prod;
  q.Open;
  if q.eof then begin
    q.SQL.Clear;
    q.Params.Clear;
    q.SQL.Add('INSERT INTO PROD VALUES (:KCOD_PROD,:KCOD_CAT,:KDESCRICAO,:KFOTO,:KCARAC)');
    q.ParambyName('kcod_prod').asstring:=pcod_prod;
    q.ParambyName('kcod_cat').asstring:=cod_cat;
    q.ParambyName('kdescricao').asstring:=descricao;
    q.ParambyName('kfoto').LoadFromStream(ms,ftGraphic);
    q.ParambyName('kcarac').asblob:=carac;
  end else begin
    q.SQL.Clear;
    q.Params.Clear;
    q.SQL.Add('UPDATE PROD SET COD_CAT=:KCOD_CAT, DESCRICAO=:KDESCRICAO, FOTO=:KFOTO, CARAC=:KCARAC WHERE COD_PROD=:KCOD_PROD');
    q.ParambyName('kcod_prod').asstring:=pcod_prod;
    q.ParambyName('kcod_cat').asstring:=cod_cat;
    q.ParambyName('kdescricao').asstring:=descricao;
    q.ParambyName('kfoto').LoadFromStream(ms,ftGraphic);
    q.ParambyName('kcarac').asblob:=carac;
  end;
    try
    q.ExecSQL;
    result:=1;
  except
    result:=0;
  end;
  ms.Free;
  q.close;
  q.Free;
end;

function TProd.recuperar;
var q:TQuery;
    ms:TmemoryStream;
    bf:TBlobField;

begin
  q:=TQuery.Create(dm.db);
  q.Name:='prod';
  q.DatabaseName:=dm.db.DatabaseName;
  q.Close;
  q.SQL.Clear;
  q.Params.Clear;
  q.SQL.Add('SELECT * FROM PROD WHERE COD_PROD=:KCOD_PROD');
  q.ParambyName('kcod_prod').asstring:=pcod_prod;
  q.Open;
  if q.eof then begin
    cod_prod:=pcod_prod;
    descricao:='';
    result:=0;
  end else begin
    cod_prod:=pcod_prod;
    cod_cat:=q.fieldbyname('cod_cat').asstring;
    descricao:=q.fieldbyname('descricao').asstring;
    if not (q.fieldbyname('foto').isNull) then begin
      bf:=TBlobField(q.Fieldbyname('foto'));
      ms:=TmemoryStream.Create;
      bf.savetostream(ms);
      ms.position:=0;
      try
        pJpeg.LoadFromStream(ms);
      except
        pJpeg:=Nil;
      end;
      ms.free;
    end;
    carac:=q.fieldbyname('carac').asstring;
    result:=1;
  end;
  q.close;
  q.free;
end;

procedure TProd.lista;

begin
  pq.DatabaseName:=dm.db.DatabaseName;
  pq.Close;
  pq.SQL.Clear;
  pq.Params.Clear;
  pq.SQL.Add('SELECT * FROM PROD WHERE DESCRICAO LIKE :KDESCRICAO ORDER BY DESCRICAO');
  pq.ParambyName('kdescricao').asstring:=pdescricao+'%';
  pq.Open;
end;

function TProd.excluir;
var q:TQuery;
begin
  q:=TQuery.Create(dm.db);
  q.Name:='prod';
  q.DatabaseName:=dm.db.DatabaseName;
  q.Close;
  q.SQL.Clear;
  q.Params.Clear;
  q.SQL.Add('DELETE FROM PROD WHERE COD_PROD=:KCOD_PROD');
  q.ParambyName('kcod_prod').asstring:=pcod_prod;
  try
    q.ExecSQL;
    result:=1;
  except
    result:=0;
  end;
  q.Close;
  q.Free;
end;


end.
